<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
    <title>Agent管理</title>
</head>
<link rel="stylesheet" type="text/css" href="css/ext-all.css"/>
<link rel="stylesheet" type="text/css" href="css/ext-zh_CN.css"/>
<script type="text/javascript" src="js/ext-all.js"></script>
<script type="text/javascript" src="js/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/base.js"></script>
<body>
<script type="text/javascript">
    var win;
    var returnValue;
    Ext.onReady(function () {
        Ext.getBody().mask('<p>页面载入中...</p>');//页面笼罩效果

        var agentStore = Ext.create('Ext.data.Store', {
            storeId: 'agentStore',
            autoLoad: false,//true为自动加载
            loading: false,//自动加载时必须为true
            pageSize: 50,
            fields: ['AGENT_ID_', 'AGENT_CODE_', 'AGENT_NAME_', 'AGENT_URL_', 'AGENT_ARGUMENTS_', 'AGENT_STATE_', 'STATE_', 'AGENT_COST_', 'AGENT_SOURCE_'],
            proxy: {
                url: 'agentManage',
                type: 'ajax',
                async: true,//false=同步.
                actionMethods: {
                    read: 'POST'
                },
                extraParams: {},
                reader: {
                    type: 'json',
                    root: 'result',
                    totalProperty: 'total'
                }
            }
        });

        var buttonPanel = Ext.create('Ext.Panel', {
            id: 'buttonPanel',
            defaults: {
                style: 'margin: 2px;'
            },
            items: [{
                id: 'select',
                xtype: 'button',
                text: '查询',
                handler: _selectAgent
            }, {
                id: 'insert',
                xtype: 'button',
                text: '新增',
                handler: _preInsertAgent
            }, {
                id: 'update',
                xtype: 'button',
                text: '修改',
                handler: _preUpdateAgent
            }, {
                id: 'delete',
                xtype: 'button',
                text: '删除',
                handler: _deleteAgent
            }, {
                id: 'start',
                xtype: 'button',
                text: '启动Agent',
                handler: _startAgent
            }, {
                id: 'close',
                xtype: 'button',
                text: '关闭Agent',
                handler: _killAgent
            }, {
                id: 'startAll',
                xtype: 'button',
                text: '启动所有Agent',
                handler: _startAllAgent
            }, {
                id: 'closeAll',
                xtype: 'button',
                text: '关闭所有Agent',
                handler: _killAllAgent
            }]
        });

        var formPanel = Ext.create('Ext.form.Panel', {
            id: 'formPanel',
            layout: 'column',
            frame: true,
            defaults: {
                labelAlign: 'right',
                labelWidth: 100,
                inputWidth: 140,
                margin: '4,0,0,0'
            },
            items: [{
                xtype: 'textfield',
                id: 'AGENT_NAME_',
                name: 'AGENT_NAME_',
                fieldLabel: '智能体名称',
                maxLength: 20
            }]
        });

        var gridPanel = Ext.create('Ext.grid.Panel', {
            id: 'gridPanel',
            store: agentStore,
            columnLines: true,
            frame: true,
            selModel: {
                selType: 'checkboxmodel',
                mode: 'SIMPLE'
            },
            columns: [{
                text: '序号',
                xtype: 'rownumberer',
                width: 60,
                align: 'center'
            }, {
                text: '智能体编码',
                dataIndex: 'AGENT_CODE_',
                align: 'center',
                flex: 1
            }, {
                text: '智能体名称',
                dataIndex: 'AGENT_NAME_',
                align: 'center',
                flex: 1
            }, {
                text: '智能体路径',
                dataIndex: 'AGENT_URL_',
                align: 'center',
                flex: 1
            }, {
                text: '智能体启动参数',
                dataIndex: 'AGENT_ARGUMENTS_',
                align: 'center',
                flex: 1
            }, {
                text: '设备智能体价值(元)',
                dataIndex: 'AGENT_COST_',
                align: 'center',
                flex: 1
            }, {
                text: '设备智能体所属',
                dataIndex: 'AGENT_SOURCE_',
                align: 'center',
                flex: 1
            }, {
                text: '智能体状态',
                dataIndex: 'AGENT_STATE_',
                align: 'center',
                flex: 1,
                renderer: function (value, metaData, record, rowIndex, colIndex, store, view) {
                    switch (value) {
                        case '0':
                            return '未启动';
                            break;
                        case '1':
                            return '启动';
                            break;
                        default:
                            return value;
                            break;
                    }
                }
            }],
            viewConfig: {
                emptyText: '<div style="text-align: center; padding-top: 50px; font: italic bold 20px Microsoft YaHei;">没有菜单数据</div>',
                enableTextSelection: true
            },
            dockedItems: [{
                xtype: 'pagingtoolbar',
                store: agentStore,
                dock: 'bottom',
                displayInfo: true
            }]
        });

        Ext.create('Ext.container.Viewport', {
            layout: {
                type: 'border',
                regionWeights: {
                    west: -1,
                    north: 1,
                    south: 1,
                    east: -1
                }
            },
            defaults: {
                border: false
            },
            items: [{
                region: 'north',
                items: [buttonPanel, formPanel]
            }, {
                region: 'center',
                layout: 'fit',
                items: [gridPanel]
            }]
        });

        _init();
    });

    function _init() {
        for (var i = 0, len = Ext.data.StoreManager.getCount(); i < len; i++) {
            if (Ext.data.StoreManager.getAt(i).isLoading()) {
                return;
            }
        }

        _selectAgent();
        Ext.getBody().unmask();
    }

    function _selectAgent() {
        var agentStore = Ext.data.StoreManager.lookup('agentStore');
        agentStore.proxy.extraParams = {
            'STATE_': '1',
            'AGENT_NAME_': Ext.getCmp('AGENT_NAME_').getValue()
        };
        agentStore.currentPage = 1;
        agentStore.load();
    }

    //启动Agent
    function _startAgent() {
        var records = Ext.getCmp('gridPanel').getSelectionModel().getSelection();
        if (records.length == 0) {
            Ext.MessageBox.alert('警告', '请选择需要启动Agent的设备', Ext.MessageBox.WARNING);
            return;
        }
        var AGENT_LIST = new Array();
        //     判断Agent状态
        for (var i = 0, len = records.length; i < len; i++) {
            if (records[i].data.AGENT_STATE_ != '0') {
                Ext.MessageBox.alert('警告', records[i].data.AGENT_NAME_ + 'Agent已启动', Ext.MessageBox.WARNING);
                return;
            }
            var AGENT_LIST1 = new Array();
            AGENT_LIST1.push(
                records[i].data.AGENT_CODE_,
                records[i].data.AGENT_URL_,
                records[i].data.AGENT_ARGUMENTS_ == null ? ' ' : records[i].data.AGENT_ARGUMENTS_
            );
            AGENT_LIST.push(AGENT_LIST1);
        }

        Ext.MessageBox.show({
            title: '请选择',
            msg: '是否启动智能体',
            buttons: Ext.MessageBox.YESNO,
            icon: Ext.MessageBox.QUESTION,
            fn: function (btn) {
                if (btn == 'yes') {
                    Ext.getBody().mask('<p>Agent启动中...</p>');
                    Ext.Ajax.request({
                        url: 'startAgent',
                        async: true,
                        params: {
                            'AGENT_LIST': AGENT_LIST
                        },
                        callback: function (options, success, response) {
                            if (success) {
                                var data = Ext.decode(response.responseText);
                                if (data.success) {
                                    for (var i = 0, len = records.length; i < len; i++) {
                                        records[i].set('AGENT_STATE_', '1');
                                    }
                                    Toast.alert('信息', '启动成功', 1000);
                                } else {
                                    _selectAgent();
                                    Ext.MessageBox.alert('错误', '启动Agent失败', Ext.MessageBox.ERROR);
                                }
                            } else {
                                Ext.MessageBox.alert('错误', '服务器错误', Ext.MessageBox.ERROR);
                            }
                            Ext.getBody().unmask();
                        }
                    });
                }
            }
        });
    }

    //关闭Agent
    function _killAgent() {
        var records = Ext.getCmp('gridPanel').getSelectionModel().getSelection();
        if (records.length == 0) {
            Ext.MessageBox.alert('警告', '请选择需要启动Agent的设备', Ext.MessageBox.WARNING);
            return;
        }
        var AGENT_LIST = new Array();
        //判断Agent状态
        for (var i = 0, len = records.length; i < len; i++) {
            if (records[i].data.AGENT_STATE_ != '1') {
                Ext.MessageBox.alert('警告', records[i].data.AGENT_NAME_ + 'Agent未启动', Ext.MessageBox.WARNING);
                return;
            }
            AGENT_LIST.push(records[i].data.AGENT_CODE_);
        }

        Ext.MessageBox.show({
            title: '请选择',
            msg: '是否关闭智能体',
            buttons: Ext.MessageBox.YESNO,
            icon: Ext.MessageBox.QUESTION,
            fn: function (btn) {
                if (btn == 'yes') {
                    Ext.Ajax.request({
                        url: 'killAgent',
                        async: true,
                        params: {
                            'AGENT_LIST': AGENT_LIST
                        },
                        callback: function (options, success, response) {
                            if (success) {
                                var data = Ext.decode(response.responseText);
                                if (data.success) {
                                    for (var i = 0, len = records.length; i < len; i++) {
                                        records[i].set('AGENT_STATE_', '0');
                                    }
                                    Toast.alert('信息', '关闭成功', 1000);
                                } else {
                                    Ext.MessageBox.alert('错误', "关闭失败", Ext.MessageBox.ERROR);
                                }
                            } else {
                                Ext.MessageBox.alert('错误', '服务器错误', Ext.MessageBox.ERROR);
                            }
                        }
                    });
                }
            }
        });
    }

    //启动所有agent
    function _startAllAgent() {
        Ext.MessageBox.show({
            title: '请选择',
            msg: '是否开启所有的智能体',
            buttons: Ext.MessageBox.YESNO,
            icon: Ext.MessageBox.QUESTION,
            fn: function (btn) {
                if (btn == 'yes') {
                    Ext.getBody().mask('<p>Agent启动中...</p>');
                    Ext.Ajax.request({
                        url: 'startAllAgents',
                        async: true,
                        params: {
                            'STATE_': '1',
                            'AGENT_STATE_': '0'
                        },
                        callback: function (options, success, response) {
                            if (success) {
                                var data = Ext.decode(response.responseText);
                                if (data.success) {
                                    var agentStore = Ext.data.StoreManager.lookup('agentStore');
                                    for (var i = 0, len = agentStore.getCount(); i < len; i++) {
                                        agentStore.getAt(i).set('AGENT_STATE_', '1');
                                    }
                                    Toast.alert('信息', '开启成功', 1000);
                                } else {
                                    _selectAgent();
                                    Ext.MessageBox.alert('错误', '启动Agent失败', Ext.MessageBox.ERROR);
                                }
                            } else {
                                Ext.MessageBox.alert('错误', '服务器错误', Ext.MessageBox.ERROR);
                            }
                            Ext.getBody().unmask();
                        }
                    });
                }
            }
        });
    }

    //关闭所有agent
    function _killAllAgent() {
        Ext.MessageBox.show({
            title: '请选择',
            msg: '是否关闭所有的智能体',
            buttons: Ext.MessageBox.YESNO,
            icon: Ext.MessageBox.QUESTION,
            fn: function (btn) {
                if (btn == 'yes') {
                    Ext.Ajax.request({
                        url: 'killAllAgents',
                        async: true,
                        params: {
                            'STATE_': '1',
                            'AGENT_STATE_': '1'
                        },
                        callback: function (options, success, response) {
                            if (success) {
                                var data = Ext.decode(response.responseText);
                                if (data.success) {
                                    var agentStore = Ext.data.StoreManager.lookup('agentStore');
                                    for (var i = 0, len = agentStore.getCount(); i < len; i++) {
                                        agentStore.getAt(i).set('AGENT_STATE_', '0');
                                    }
                                    Toast.alert('信息', '关闭成功', 1000);
                                } else {
                                    Ext.MessageBox.alert('错误', "关闭失败", Ext.MessageBox.ERROR);
                                }
                            } else {
                                Ext.MessageBox.alert('错误', '服务器错误', Ext.MessageBox.ERROR);
                            }
                        }
                    });
                }
            }
        });
    }

    //新增
    function _preInsertAgent() {
        returnValue = null;
        win = Ext.create('Ext.window.Window', {
            title: '新增Agent',
            modal: true,
            autoShow: true,
            maximized: false,
            maximizable: true,
            width: 560,
            height: 420,
            html: '<iframe src="preInsertAgent" style="width: 100%; height: 100%;" frameborder="0"></iframe>',
            listeners: {
                close: function (panel, eOpts) {
                    if (returnValue != null) {
                        Ext.data.StoreManager.lookup('agentStore').add(returnValue);
                        Toast.alert('信息', '新增成功', 1000);
                    }
                }
            }
        });
    }

    //修改
    function _preUpdateAgent() {
        var records = Ext.getCmp('gridPanel').getSelectionModel().getSelection();
        if (records.length != 1) {
            Ext.MessageBox.alert('警告', '请选择一条要修改的数据!', Ext.MessageBox.WARNING);
            return;
        }

        returnValue = null;
        win = Ext.create('Ext.window.Window', {
            title: '修改Agent',
            modal: true,
            autoShow: true,
            maximized: false,
            maximizable: true,
            width: 560,
            height: 420,
            html: '<iframe src="preUpdateAgent?AGENT_ID_=' + records[0].get('AGENT_ID_') + '" style="width: 100%; height: 100%;" frameborder="0"></iframe>',
            listeners: {
                close: function (panel, eOpts) {
                    if (returnValue != null && returnValue.success) {
                        var agent = returnValue.result;
                        for (var key in agent) {
                            records[0].set(key, agent[key]);
                        }
                        Toast.alert('信息', '修改成功', 1000);
                    }
                }
            }
        });
    }

    //删除Agent
    function _deleteAgent() {
        var records = Ext.getCmp('gridPanel').getSelectionModel().getSelection();
        if (records.length == 0) {
            Ext.MessageBox.alert('警告', '请选择需要删除的Agent', Ext.MessageBox.WARNING);
            return;
        }
        var AGENT_LIST = new Array();
        for (var i = 0, len = records.length; i < len; i++) {
            if (records[i].data.AGENT_STATE_ == '1') {
                Ext.MessageBox.alert('警告', records[i].data.AGENT_NAME_ + 'Agent启动中,无法删除', Ext.MessageBox.WARNING);
                return;
            }
            AGENT_LIST.push(records[i].data.AGENT_ID_);
        }
        Ext.MessageBox.show({
            title: '请选择',
            msg: '是否删除选中的智能体',
            buttons: Ext.MessageBox.YESNO,
            icon: Ext.MessageBox.QUESTION,
            fn: function (btn) {
                if (btn == 'yes') {
                    Ext.Ajax.request({
                        url: 'deleteAgent',
                        async: true,
                        params: {
                            'AGENT_LIST': AGENT_LIST
                        },
                        callback: function (options, success, response) {
                            if (success) {
                                var data = Ext.decode(response.responseText);
                                if (data.success) {
                                    var agentStore = Ext.data.StoreManager.lookup('agentStore');
                                    for (var i = 0, len = records.length; i < len; i++) {
                                        agentStore.remove(records[i]);
                                    }
                                    Toast.alert('信息', '删除成功', 1000);
                                } else {
                                    Ext.MessageBox.alert('错误', "删除失败", Ext.MessageBox.ERROR);
                                }
                            } else {
                                Ext.MessageBox.alert('错误', '服务器错误', Ext.MessageBox.ERROR);
                            }
                        }
                    });
                }
            }
        });
    }
</script>
</body>
</html>
